package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult pageSelectDish(DishPageQueryDTO dishPageQueryDTO) {
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishPageQueryDTO, dishVO);
        Page<DishVO> dishvo = dishMapper.pageSelectDish(dishVO);
        return new PageResult(dishvo.getTotal(), dishvo.getResult());
    }

    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = dishMapper.selectDishById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishIdFlavors(dishVO.getId());
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public List<Dish> selectByCategoryId(Integer categoryId) {
        return dishMapper.selectDishByCategoryId(categoryId);
    }

    @Override
    public void statusStartOrStop(Integer status, Long id) {
        //updateTime(LocalDateTime.now()).
        //updateUser(BaseContext.getCurrentId()).
        if (status == StatusConstant.DISABLE) {
            SetmealDish setmealDish = setmealDishMapper.selectByDishId(id);
            SetmealVO setmealVO = setmealMapper.byIdSelectSetmeal(setmealDish.getSetmealId());
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealVO, setmeal);
            setmeal.setStatus(status);
            setmealMapper.updateSetmeal(setmeal);
        }
        dishMapper.updateDish(Dish.builder().
                status(status).
                id(id).build());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()) {
            dishFlavorMapper.deleteByIdDishFlavors(dish.getId());
            flavors.forEach(a -> {
                a.setDishId(dish.getId());
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    @Override
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insertDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        ids.forEach(id -> {
            DishVO dishVO = dishMapper.selectDishById(id);
            if (dishVO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            SetmealDish setmealDish = setmealDishMapper.selectByDishId(id);
            if (setmealDish != null) {
                throw new SetmealEnableFailedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            dishMapper.deleteDish(id);
            dishFlavorMapper.deleteByIdDishFlavors(id);
        });
    }
}
